<template>
  <div class="main">
    <div class="header">
      <div class="logo_text">
<!--        <img :src="customGlobal.loginIcon" alt="icon">-->
        <img src="../../assets/login/logobg.svg" alt="icon">
      </div>
    </div>
    <div class="right">
      <div class="desc" v-if="retrieveShow">
        <a-form-model class="user-layout-login" @keyup.enter.native="handleSubmit">
          <!-- <a-tabs :activeKey="customActiveKey" :tabBarStyle="{ textAlign: 'center', borderBottom: 'unset' }"  @change="handleTabClick"> -->
          <!--        <a-tab-pane key="tab1" tab="账号密码登录">
                    <login-account ref="alogin" @validateFail="validateFail" @success="requestSuccess" @fail="requestFailed"></login-account>
                  </a-tab-pane>-->
          <!-- <a-tab-pane key="tab2" tab="手机号登录"> -->
          <p style="display: flex;justify-content: space-between;align-items: center;">
            <span style="color: #666666;font-size: 24px;">登录</span>
            <router-link :to="{ name: 'PasswordSettings'}" class="forge-password">注册账户</router-link>
          </p>
          <login-phone ref="plogin" @validateFail="validateFail" @success="requestSuccess" @fail="requestFailed"></login-phone>
          <p style='text-align: right;'><span class="forge-password" @click="retrievePassword" style="cursor: pointer;">忘记密码</span></p>
          <!--    </a-tab-pane>
            </a-tabs> -->
          <!--			  <p style="text-align: left;">-->
          <!--			    <a-checkbox @change="handleRememberMeChange" default-checked>自动登录</a-checkbox>-->
          <!--			  </p>-->
          <a-form-item style="margin-top: 50px;">
            <a-button size="large"
                      type="primary"  htmlType="submit"
                      class="login-button"
                      :loading="loginBtn"
                      @click.stop.prevent="handleSubmit"
                      :disabled="loginBtn"
            >确定</a-button>
          </a-form-item>
          <div class="xiey">
            <a-checkbox value="1" @change="handleReserve" default-checked>
              已阅读并接受
              <a class="fontsize" target="_blank" href="//zxcloud.oss-cn-hangzhou.aliyuncs.com/user/files/%E6%95%B0%E5%AD%97%E8%AF%81%E4%B9%A6%E4%BD%BF%E7%94%A8%E5%8D%8F%E8%AE%AE.pdf">《数字证书使用协议》</a>
              及 <a class="fontsize" target="_blank" href="//zxcloud.oss-cn-hangzhou.aliyuncs.com/user/files/%E5%8B%A4%E7%A8%8E%E5%AE%9D%E9%9A%90%E7%A7%81%E6%94%BF%E7%AD%96%EF%BC%88%E4%BF%A1%E6%81%AF%E6%8E%88%E6%9D%83%E7%89%88%EF%BC%89.pdf">《勤税宝隐私协议与服务协议》</a>
            </a-checkbox >
          </div>
        </a-form-model>
      </div>
      <div class="descon" v-if="!retrieveShow">
        <forget-password @showBack="back"></forget-password>
      </div>
      <p class="saveRecord" @click="toGo()">
        <span>Copyright 2022 © 中雄信息（荆州）有限公司 鄂ICP备19020675号-1</span>
        <span>信息系统安全三级保护备案 421000-33002-22001</span>
      </p>
    </div>
    <two-step-captcha v-if="requiredTwoStepCaptcha" :visible="stepCaptchaVisible" @success="stepCaptchaSuccess" @cancel="stepCaptchaCancel"></two-step-captcha>
    <login-select-tenant ref="loginSelect" @success="loginSelectOk"></login-select-tenant>
    <!--    <third-login ref="thirdLogin"></third-login>-->
  </div>
</template>

<script>
import Vue from 'vue'
import { ACCESS_TOKEN, ENCRYPTED_STRING } from '@/store/mutation-types'
import ThirdLogin from './third/ThirdLogin'
import LoginSelectTenant from './LoginSelectTenant'
import TwoStepCaptcha from '@/components/tools/TwoStepCaptcha'
import ForgetPassword from './forgetPassword'
import { getEncryptedString } from '@/utils/encryption/aesEncrypt'
import { timeFix } from '@/utils/util'
import { getAction } from '@/api/manage'
import { getUserCtl } from '@/api/login'
import LoginAccount from './LoginAccount'
import LoginPhone from './LoginPhone'

export default {
  components: {
    LoginSelectTenant,
    TwoStepCaptcha,
    ThirdLogin,
    LoginAccount,
    LoginPhone,
    ForgetPassword
  },
  data () {
    return {
      customActiveKey: 'tab2',
      rememberMe: true,
      radioChange: true,
      loginBtn: false,
      requiredTwoStepCaptcha: false,
      stepCaptchaVisible: false,
      encryptedString:{
        key:"",
        iv:"",
      },
      //忘记密码
      retrieveShow: true,
      form: {
        phone: '',
        captcha: ''
      },
      status: {
        time: 60,
        smsSendBtn: false,
      },
      rules:{
        phone: [
          { required: true, message: '请输入手机号码!' },
          { validator: this.validatePhone }
        ],
        captcha: [{
          required: true, message: '请输入验证码!'
        }]
      }
    }
  },
  created() {
    Vue.ls.remove(ACCESS_TOKEN)
    this.getRouterData();
    this.rememberMe = true
  },
  methods:{
    handleTabClick(key){
      this.customActiveKey = key
    },
    handleRememberMeChange(e){
      this.rememberMe = e.target.checked
    },
    handleReserve(e){
      this.radioChange = e.target.checked;
    },
    /**跳转到登录页面的参数-账号获取*/
    getRouterData(){
      this.$nextTick(() => {
        let temp = this.$route.params.username || this.$route.query.username || ''
        if (temp) {
          this.$refs.alogin.acceptUsername(temp)
        }
      })
    },
    //备案号跳转
    toGo(){
      window.open('https://beian.miit.gov.cn/#/Integrated/index','_blank');
    },
    //登录
    handleSubmit () {
      if (!this.radioChange){
        this.$message.warning('请勾选同意《服务协议》和《数字证书使用协议》')
      }else{
        this.loginBtn = true;
        if (this.customActiveKey === 'tab1') {
          // 使用账户密码登录
          this.$refs.alogin.handleLogin(this.rememberMe)
        } else {
          //手机号码登录
          this.$refs.plogin.handleLogin(this.rememberMe)
        }
      }
    },
    //找回密码
    retrievePassword(){
      this.retrieveShow = false;
    },
    //返回登录
    back(){
      this.retrieveShow = true;
    },
    // 校验失败
    validateFail(){
      this.loginBtn = false;
    },
    // 登录后台成功
    requestSuccess(loginResult){
      this.$refs.loginSelect.show(loginResult)
      this.getUserCtlFun();
    },
    //登录后台失败
    requestFailed (err) {
      let description = ((err.response || {}).data || {}).message || err.message || "请求出现错误，请稍后再试"
      this.$notification[ 'error' ]({
        message: '登录失败',
        description: description,
        duration: 4,
      });
      //账户密码登录错误后更新验证码
      if(this.customActiveKey === 'tab1' && description.indexOf('密码错误')>0){
        this.$refs.alogin.handleChangeCheckCode()
      }
      this.loginBtn = false;
    },
    loginSelectOk(){
      this.loginSuccess()
    },
    //登录成功
    loginSuccess () {
      this.$router.push({ path: "/index/index" }).catch(()=>{
        console.log('登录跳转首页出错,这个错误从哪里来的')
      })
      this.$notification.success({
        message: '欢迎',
        description: `${timeFix()}，欢迎回来`,
      });
    },

    stepCaptchaSuccess () {
      this.loginSuccess()
    },
    stepCaptchaCancel () {
      this.Logout().then(() => {
        this.loginBtn = false
        this.stepCaptchaVisible = false
      })
    },
    //获取密码加密规则
    getEncrypte(){
      var encryptedString = Vue.ls.get(ENCRYPTED_STRING);
      if(encryptedString == null){
        getEncryptedString().then((data) => {
          this.encryptedString = data
        });
      }else{
        this.encryptedString = encryptedString;
      }
    },
    getUserCtlFun(){
      getUserCtl().then((res) =>{
        if(res.success){
          if(res.success){
            const data = res.result || {};
            localStorage.setItem('fundFlagInfo', data.fundFlag)
          }
        }
      })
    },

  }

}
</script>
<style lang="less" scoped>
.main{
  width: 100%;
  height: 100%;
  text-align: center;
  display: flex;
  justify-content: space-around;
  background: #F0F2F5;
  .header {
    width: 100%;
    height: 100%;
    background: url("../../assets/login/loginbg.png") left top no-repeat;
    background-size: 100% 100%;
    .logo_text{
      width: 460px;
      height: 62px;
      position: absolute;
      bottom: 40px;
      left: 40px;
      display: flex;
      align-items: center;
    }
    .logo_text img{
      width: 100%;
      height: 100%;
    }
  }
  .right{
    width: 100%;
    height: 100%;
    position: relative;
    .desc {
      width: 100%;
      height: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    //忘记密码
    .descon {
      width: 100%;
      height: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }

}
/deep/.ant-form .ant-form-horizontal .ant-form-item{
  margin-bottom: 0;
  min-height: 65px;
}

button.login-button {
  padding: 0 15px;
  font-size: 16px;
  height: 40px;
  width: 100%;
}
.xiey{
  width: 400px;
  //background: pink;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  .ant-checkbox-wrapper{
    max-width: 100%;
    text-align: left;
    line-height: 24px;
  }
  .forge-password{
    color: @primary-color;
    white-space: nowrap;
  }
}
/deep/.ant-checkbox + span{
  padding-left: 1px;
}
.saveRecord{
  position: absolute;
  bottom: 0;
  right: 0;
  left: 0;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  margin-top: 100px;
  color: #999999;
  span{
    width: 100%;
    display: block;
  }
}
.saveRecord:hover{
  cursor: pointer;
}
//忘记密码
/deep/.ant-form-item-children{
  display: flex;
  align-items: center;
  justify-content: space-between;
}
/deep/.ant-form-explain{
  text-align: left;
  margin-top: 2px;
}
.getCaptcha {
  display: block;
  height: 40px;
  background: #022EA0;
  border: none;
  color: #FFFFFF;
}
///deep/.ant-input-prefix{
//  color: #022EA0;
//}
//.user-layout-login {
//  label {
//    font-size: 14px;
//  }
//.getCaptcha {
//  display: block;
//  width: 100%;
//  height: 40px;
//}

//.forge-password {
//  font-size: 14px;
//}

//.user-login-other {
//  text-align: left;
//  margin-top: 24px;
//  line-height: 22px;
//
//  .item-icon {
//    font-size: 24px;
//    color: rgba(0,0,0,.2);
//    margin-left: 16px;
//    vertical-align: middle;
//    cursor: pointer;
//    transition: color .3s;
//
//    &:hover {
//      color: #1890ff;
//    }
//  }
//
//  .register {
//    float: right;
//  }
//}
//}

</style>

<!--<style>-->
<!--.valid-error .ant-select-selection__placeholder{-->
<!--  color: #f5222d;-->
<!--}-->
<!--</style>-->
